package com.kgcx.coupon.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.utils.DateTimeUtils;
import com.kgcx.coupon.domain.RatedDriver;
import com.kgcx.coupon.mapper.RatedDriverMapper;
import com.kgcx.coupon.rabbitmq.producer.OpcProducer;
import com.kgcx.dbutils.utils.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author 何凯
 * @version 0.1
 * @description
 * @time 2017/12/7 15:08
 * @since 0.1
 */
@Service
public class RatedDriverService {

    public static Logger logger = LoggerFactory.getLogger(RatedDriverService.class);

    @Autowired
    private RatedDriverMapper ratedDriverMapper;
    @Autowired
    private OpcProducer opcProducer;

    /**
     * 司机信誉信息分页查询
     * @param rd
     * @return
     */
    public List<RatedDriver> queryRatedDriverWithPage(RequestData rd){
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        List<RatedDriver> list = ratedDriverMapper.selectRatedDriverList(rd);
        for (RatedDriver rdDriver :list){
            opcProducer.sendRatedDriver(rdDriver);
        }
        return list;
    }

    /**
     * 新增驾驶员信誉信息
     * @param licenseId
     * @param level
     * @param testDepartment
     * @return
     */
    public Integer addRatedDriver(String licenseId,Float level, String testDepartment) {
        RatedDriver ratedDriver = new RatedDriver();
        ratedDriver.setLicenseId(licenseId);
        ratedDriver.setLevel(level);
        ratedDriver.setTestDepartment(testDepartment);

        Integer dateTime =  DateTimeUtils.convertNumberDay(LocalDateTime.now());
        Date date = new Date();
        ratedDriver.setUpdateDate(date);
        ratedDriver.setCreateTime(date);
        ratedDriver.setTestDate(dateTime);
        Integer one = ratedDriverMapper.insert(ratedDriver);
        //TODO 发送消息给监管平台
        try {
            opcProducer.sendRatedDriver(ratedDriver);
        }catch (Exception e){
            logger.error("licenseId:"+licenseId+"punish send punish error",e);
        }
        return  one;
    }

    /**
     * 更新驾驶员信誉信息
     * @param licenseId
     * @param level
     * @param testDepartment
     * @return
     */
    public Integer updateRatedDriver(String licenseId,Float level, String testDepartment){
        RatedDriver ratedDriver = new RatedDriver();
        ratedDriver.setLicenseId(licenseId);
        ratedDriver.setLevel(level);
        ratedDriver.setTestDepartment(testDepartment);
        Integer dateTime =  DateTimeUtils.convertNumberDay(LocalDateTime.now());
        ratedDriver.setTestDate(dateTime);
        Integer one = ratedDriverMapper.updateRated(ratedDriver);
        //TODO 发送消息给监管平台
       try {
           opcProducer.sendRatedUpdateDriver(ratedDriver);
       }catch (Exception e){
            logger.error("licenseId:"+licenseId+"ratedDriver send ratedDriver error",e);
        }
        return one;
    }

    /**
     * 判断驾驶证是否存在
     * @param licenseId
     * @return
     */
    public Integer existDriver(String licenseId){
        return  ratedDriverMapper.exist(licenseId);
    }

    /**
     * 根据驾驶证号查询驾驶员服务等级
     * @param licenseId
     * @return
     */
    public Float selectLevelByLicenseId(String licenseId){
        return ratedDriverMapper.selectLevelByLicenseId(licenseId);
    }
}
